﻿Public Class TietKiemForm
    Dim dt As DataTable
    Dim dt_nganhang As DataTable
    Dim dt_thunhap As DataTable
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Me.Dispose()
    End Sub

    Private Sub NganHangForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        rb3Thang.Checked = True

        dt_nganhang = NganHangDAO.GetAll()
        cbMaNganHang.DataSource = dt_nganhang
        cbMaNganHang.DisplayMember = "Ten"
        cbMaNganHang.ValueMember = "MaNganHang"

        dt_thunhap = ThuNhapDAO.GetAll()
        cbMaThuNhap.DataSource = dt_thunhap
        cbMaThuNhap.DisplayMember = "Ten"
        cbMaThuNhap.ValueMember = "MaThuNhap"

        dt = TietKiemDAO.GetAll(MainForm.current_user.MaUser)
        dg.DataSource = dt
        dg.Columns("NgayGui").HeaderText = "Ngày Gửi"
        dg.Columns("LaiSuat").HeaderText = "Lãi Suất"
        dg.Columns("KyHan").HeaderText = "Kỳ Hạn"
        dg.Columns("TuDongGiaHan").HeaderText = "Tự Động Gia Hạn"
        dg.Columns("SoTien").HeaderText = "Số Tiền"
        dg.Columns("SoTien").DefaultCellStyle.Format = "N0"
        dg.Columns("Ten").HeaderText = "Tên Ngân Hàng"
        dg.Columns("Ten1").HeaderText = "Tên Thu Nhập"
        dg.Columns("TienLaiHangThang").HeaderText = "Tiền lãi hàng tháng"
        'dg.Columns("TinhTrang").HeaderText = "Tình trạng"
        dg.Columns("Ten").DisplayIndex = 1
        dg.Columns("SoTien").DisplayIndex = 2
        dg.Columns("MaTietKiem").Visible = False
        dg.Columns("MaUser").Visible = False
        dg.Columns("MaThuNhap").Visible = False
        dg.Columns("MaThuNhap1").Visible = False
        dg.Columns("MaNganHang").Visible = False        
        dg.Columns("MaNganHang1").Visible = False
    End Sub

    Private Sub dgNganHang_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellClick
        txtSoTien.Text = dt.Rows(dg.CurrentRow.Index).Item("SoTien").ToString()
        cbMaNganHang.SelectedValue = dt.Rows(dg.CurrentRow.Index).Item("MaNganHang").ToString()
        cbMaThuNhap.SelectedValue = dt.Rows(dg.CurrentRow.Index).Item("MaThuNhap").ToString()
        dtNgayGui.Text = dt.Rows(dg.CurrentRow.Index).Item("NgayGui").ToString()
        txtLaiSuat.Text = dt.Rows(dg.CurrentRow.Index).Item("LaiSuat").ToString()
        If dt.Rows(dg.CurrentRow.Index).Item("TuDongGiaHan") = 1 Then
            cbTuDongGiaHan.Checked = True
        End If
        Select Case dt.Rows(dg.CurrentRow.Index).Item("KyHan")
            Case 1
                rb1Thang.Checked = True
            Case 3
                rb3Thang.Checked = True
            Case 6
                rb6Thang.Checked = True
            Case Else
                rb12Thang.Checked = True
        End Select
    End Sub

    Private Sub btThem_Click(sender As System.Object, e As System.EventArgs) Handles btThem.Click
        If txtSoTien.Text.Trim().Length = 0 Or Not IsNumeric(txtSoTien.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào số tiền")
            Return
        End If
        If txtLaiSuat.Text.Trim().Length = 0 Or Not IsNumeric(txtLaiSuat.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào lãi suất")
            Return
        End If
        If cbMaThuNhap.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn loại thu nhập")
            Return
        End If
        Dim dr As DataRow = dt.NewRow()
        dr("MaUser") = MainForm.current_user.MaUser
        dr("MaNganHang") = cbMaNganHang.SelectedValue
        dr("MaThuNhap") = cbMaThuNhap.SelectedValue
        dr("SoTien") = Double.Parse(txtSoTien.Text)
        dr("Ten") = dt_nganhang.Rows(cbMaNganHang.SelectedIndex)("Ten").ToString()
        dr("NgayGui") = dtNgayGui.Text
        dr("LaiSuat") = Double.Parse(txtLaiSuat.Text)
        If cbTuDongGiaHan.Checked Then
            dr("TuDongGiaHan") = 1
        Else
            dr("TuDongGiaHan") = 0
        End If
        If rb1Thang.Checked = True Then
            dr("KyHan") = 1
        ElseIf rb3Thang.Checked = True Then
            dr("KyHan") = 3
        ElseIf rb6Thang.Checked = True Then
            dr("KyHan") = 6
        Else
            dr("KyHan") = 12
        End If
        'dr("TienLaiHangThang") = dr("SoTien") * (dr("LaiSuat") / 100) / 12 * dr("KyHan")
        dr("TienLaiHangThang") = dr("SoTien") * (dr("LaiSuat") / 100) / 12
        dt.Rows.Add(dr)
        DB_Provider.Write(dt, "TietKiem")
        dt = TietKiemDAO.GetAll(MainForm.current_user.MaUser)
        dg.DataSource = dt
    End Sub

    Private Sub btXoa_Click(sender As System.Object, e As System.EventArgs) Handles btXoa.Click
        If dg.CurrentRow.Index >= 0 Then
            dt.Rows(dg.CurrentRow.Index).Delete()
            DB_Provider.Write(dt, "TietKiem")
        End If
    End Sub

    Private Sub btSua_Click(sender As System.Object, e As System.EventArgs) Handles btSua.Click
        If txtSoTien.Text.Trim().Length = 0 Or Not IsNumeric(txtSoTien.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào số tiền")
            Return
        End If
        If txtLaiSuat.Text.Trim().Length = 0 Or Not IsNumeric(txtLaiSuat.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào lãi suất")
            Return
        End If
        If cbMaThuNhap.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn loại thu nhập")
            Return
        End If
        Dim index As Integer = dg.CurrentRow.Index
        If (index > -1) Then
            Double.TryParse(txtSoTien.Text, dt.Rows(index).Item("SoTien"))
            dt.Rows(index).Item("MaNganHang") = cbMaNganHang.SelectedValue
            dt.Rows(index).Item("MaThuNhap") = cbMaThuNhap.SelectedValue
            dt.Rows(index).Item("Ten") = dt_nganhang.Rows(cbMaNganHang.SelectedIndex)("Ten").ToString()
            dt.Rows(index).Item("NgayGui") = dtNgayGui.Text
            Double.TryParse(txtLaiSuat.Text, dt.Rows(index).Item("LaiSuat"))
            dt.Rows(index).Item("TienLaiHangThang") = dt.Rows(index).Item("SoTien") * (dt.Rows(index).Item("LaiSuat") / 100) / 12
            If cbTuDongGiaHan.Checked Then
                dt.Rows(index).Item("TuDongGiaHan") = 1
            Else
                dt.Rows(index).Item("TuDongGiaHan") = 0
            End If
            If rb1Thang.Checked = True Then
                dt.Rows(index).Item("KyHan") = 1
            ElseIf rb3Thang.Checked = True Then
                dt.Rows(index).Item("KyHan") = 3
            ElseIf rb6Thang.Checked = True Then
                dt.Rows(index).Item("KyHan") = 6
            Else
                dt.Rows(index).Item("KyHan") = 12
            End If
            DB_Provider.Write(dt, "TietKiem")
            dt = TietKiemDAO.GetAll(MainForm.current_user.MaUser)
            dg.DataSource = dt
        End If
    End Sub

    Private Sub dg_CellFormatting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dg.CellFormatting
        For Each dr As DataGridViewRow In dg.Rows
            dr.Cells("KyHan").Style.Format = dr.Cells("KyHan").Value & " Tháng"
            dr.Cells("LaiSuat").Style.Format = dr.Cells("LaiSuat").Value & "%"
            If dr.Cells("TuDongGiaHan").Value = 0 Then
                dr.Cells("TuDongGiaHan").Style.Format = "Không"
            Else
                dr.Cells("TuDongGiaHan").Style.Format = "Có"
            End If
        Next
    End Sub

    Private Sub btnThemNganHang_Click(sender As System.Object, e As System.EventArgs) Handles btnThemNganHang.Click
        Dim f As New NganHangForm
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            dt_nganhang = NganHangDAO.GetAll()
            cbMaNganHang.DataSource = dt_nganhang
            cbMaNganHang.DisplayMember = "Ten"
            cbMaNganHang.ValueMember = "MaNganHang"
        End If
    End Sub

    Private Sub btnThemThuNhap_Click(sender As System.Object, e As System.EventArgs) Handles btnThemThuNhap.Click
        Dim f As New ThuNhapForm
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            dt_thunhap = ThuNhapDAO.GetAll()
            cbMaThuNhap.DataSource = dt_thunhap
            cbMaThuNhap.DisplayMember = "Ten"
            cbMaThuNhap.ValueMember = "MaThuNhap"
        End If        
    End Sub
End Class